package com.ssps.front.tenant2.service2.impl2;

import com.ssps.entity.*;
import com.ssps.front.tenant2.service2.Tenant2SignService;
import com.ssps.front.tenant2.utils2.Tenant2Utils;
import com.ssps.mapper.RoleMapper;
import com.ssps.mapper.UserInfoMapper;
import com.ssps.mapper.UserMapper;
import com.ssps.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: peng
 */
@Transactional
@Service
public class Tenant2SignServiceImpl implements Tenant2SignService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Override
    public List<Role> initAddUser2(RoleExample example) {
        roleMapper.selectByExample(example);
        return roleMapper.selectByExample(example);
    }

    @Override
    public Integer addUser(User user, String rid,String othercode) {

        Integer flag = 200;
        UserExample userExample = new UserExample();
        UserExample.Criteria userExampleCriteria = userExample.createCriteria();
        userExampleCriteria.andPhoneEqualTo(user.getPhone());
        try {
            List<User> example = userMapper.selectByExample(userExample);

            if (!example.isEmpty()) {
                /**
                 * 手机号已注册210
                 */
                flag = 210;
            } else {
                String accountZz = "^[\\u4E00-\\u9FA5A-Za-z0-9_\\s]{4,12}$";
                String phoneZz = "^1(3|4|5|7|8)\\d{9}$";
                String pwdZz = "^[A-Za-z0-9]{6,12}$";
                if (!user.getAcount().matches(accountZz)) {
                    /**
                     * 账号格式错误 220
                     */
                    flag = 220;
                }else{
                    /**
                     * 账号格式正确
                     * 手机号格式错误 230
                     */
                    if (!user.getPhone().matches(phoneZz)){
                        flag = 230;
                    }else{
                        /**
                         * 账号格式正确
                         * 手机号格式正确
                         * 密码格式错误
                         */
                        if(!user.getPwd().matches(pwdZz)){
                            flag = 240;
                        }
                    }
                }

                /**
                 * 账号，手机号，密码格式无误
                 * 添加成功200
                 */
                if (user.getAcount().matches(accountZz) && user.getPhone().matches(phoneZz) && user.getPhone().matches(pwdZz)) {
                    /**
                     * 添加用户
                     */
                    user.setSalt(Tenant2Utils.getSalt());
                    user.setPwd(Tenant2Utils.getMD5(user.getPwd(), user.getSalt()));
                    user.setStatus("未通过");
                    userMapper.insertSelective(user);
                    /**
                     * 添加user_role
                     */
                    Integer uid = userMapper.selectByExample(userExample).get(0).getUid();
                    UserRoleKey userRoleKey = new UserRoleKey();
                    userRoleKey.setUid(uid);
                    userRoleKey.setRid(Integer.parseInt(rid));
                    userRoleMapper.insertSelective(userRoleKey);

                    /**
                     * 添加userInfo
                     */
                    UserInfo userInfo = new UserInfo();
                    userInfo.setUid(uid);
                    userInfo.setMycode(Tenant2Utils.getMyCode());
                    if(othercode != null && !othercode.equals("")){
                        userInfo.setOthercode(othercode);
                    }
                    userInfoMapper.insertSelective(userInfo);
                    flag = 200;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            /**
             * 有异常20
             */
            flag = 250;
        } finally {
            return flag;
        }
    }
}
